#!/bin/bash -xe

# WELCOME TO THE KUBERNETES CLEANUP SCRIPT, HOPE YOU ENJOY IT :)

function usage() {
  echo "usage: k8s-cleanup $option [all / statefulsets / replicasets / deployments / services / persitentvolumeclaims / persitentvolumes / pods / configmaps / secrets]"
  exit $1
}

function statefulsets()
{
    statefulsets=$(kubectl get statefulsets | cut -d " " -f1 | sed 's/NAME//g')
    for statefulset in $statefulsets;
    do
        kubectl delete statefulset.apps/$statefulset
    done
}

function services()
{
    services=$(kubectl get services | cut -d " " -f1 | sed 's/NAME//g')
    for service in $services;
    do
        if [ $service == "kubernetes" ];
        then 
            echo "Service: \"kubernetes\" unchanged"
        else
            kubectl delete service/$service
        fi
    done
}

function replicasets()
{
    replicasets=$(kubectl get replicasets | cut -d " " -f1 | sed 's/NAME//g')
    for replicaset in $replicasets;
    do
        kubectl delete replicaset.app/$replicaset
    done
}

function deployments()
{
    deployments=$(kubectl get deployments | cut -d " " -f1 | sed 's/NAME//g')
    for deployment in $deployments;
    do
        kubectl delete deployment/$deployment
    done
}

function persitentvolumeclaims()
{
    pvcs=$(kubectl get pvc | cut -d " " -f1 | sed 's/NAME//g')
    for pvc in $pvcs;
    do
        kubectl delete pvc $pvc
    done
}

function persitentvolumes()
{
    pvs=$(kubectl get pv | cut -d " " -f1 | sed 's/NAME//g')
    for pv in $pvs;
    do
        kubectl delete pv $pv
    done
}

function pods()
{
    pods=$(kubectl get pods | cut -d " " -f1 | sed 's/NAME//g')
    for pod in $pods;
    do
        kubectl delete pod/$pod
    done
}

function configmaps()
{
    configmaps=$(kubectl get configmaps | cut -d " " -f1 | sed 's/NAME//g')
    for configmap in $configmaps;
    do
         kubectl delete configmaps $configmap
    done
}

function secrets()
{
    secrets=$(kubectl get secrets | cut -d " " -f1 | sed 's/NAME//g')
    for secret in $secrets;
    do
        if [ $secret == "default-token-8nx58" ];
        then 
            echo "Secret: \"default-token-8nx58\" unchanged"
        else
            kubectl delete secrets $secret
        fi
    done
}

for arg in "$@"
do
    case $arg in
        "all"|"everything")
            # StatefulSets
            statefulsets;
            # ReplicaSets
            replicasets;
            # Services
            services;
            # Deployments
            deployments;
            # PersistentVolumes
            persitentvolumeclaims;
            # Configmaps
            configmaps;
            # Secrets
            secrets;
            
            echo "K8S properly cleaned, ready to start from scratch !"
           ;;
        "statefulset"|"statefulsets"|"stateful")
            statefulsets
            
            echo "K8S statefulsets properly purged !"
           ;;
        "replica"|"replicas"|"replicaset"|"replicaset")
            replicasets
            
            echo "K8S replicasets properly purged !"
           ;;
        "svc"|"svcs"|"service"|"services")
            services

            echo "K8S services properly purged !"
           ;;
        "deployment"|"deployments")
            deployments

            echo "K8S deployments properly purged !"
           ;;
        "pv"|"pvs"|"persistentvolumes")
            persistentvolumes

            echo "K8S persistentvolumes properly purged !"
           ;;
        "pvc"|"pvcs"|"persitentvolumeclaims")
            persitentvolumeclaims

            echo "K8S persitentvolumeclaims properly purged !"
           ;;
        "pods"|"pod")
            pods

            echo "K8S pods properly purged !"
           ;;
        "configmap"|"configmaps"|"configs"|"confs")
            configmaps

            echo "K8S configmaps properly purged !"
           ;;
        "secret"|"secrets")
            secrets

            echo "K8S secrets properly purged !"
           ;;
        \?|*)
            usage 1
           ;;
   esac
done






